package com.zmn.uac.client;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.UacException;
import com.zmn.uuc.common.constant.DubboConsts;
import com.zmn.uuc.common.dio.captcha.SendCaptchaDIO;
import com.zmn.uuc.common.dio.captcha.ValidateCaptchaDIO;
import com.zmn.uuc.dubbo.interfaces.captcha.CaptchaRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @title:短信验证码Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 21:17
 */
@Slf4j
@Component
public class CaptchaClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private CaptchaRemoteService captchaRemoteService;

    /**
     * 检查短信验证码
     * @param vlidateCaptchaDIO
     * @return
     */
    public Boolean validateCaptcha(ValidateCaptchaDIO vlidateCaptchaDIO){
        log.info("检查短信验证码入参：[{}]",vlidateCaptchaDIO);
        ResponseDTO<Boolean> validateResponse = captchaRemoteService.validateCaptcha(vlidateCaptchaDIO);
        if(!validateResponse.isSuccess()){
            log.error("检查短信验证码请求失败，失败原因：{}", JSON.toJSONString(validateResponse));
            return Boolean.FALSE;
        }

        Boolean validateStatus = validateResponse.getData();
        log.info("检查短信验证码返回response：{}",validateStatus);

        return validateStatus;
    }


    /**
     * 发送短信验证码
     * @param sendCaptchaDIO
     * @return
     */
    public Boolean sendCaptcha(SendCaptchaDIO sendCaptchaDIO){
        log.info("发送短信验证码入参：[{}]",sendCaptchaDIO);
        ResponseDTO<Boolean> sendStatusResponse = captchaRemoteService.sendCaptcha(sendCaptchaDIO);
        if(!sendStatusResponse.isSuccess()){
            log.error("发送短信验证码请求失败，失败原因：{}",sendStatusResponse.getMessage());
            throw new UacException(sendStatusResponse.getMessage());
        }

        Boolean sendStatus = sendStatusResponse.getData();
        log.info("发送短信验证码返回response：{}",sendStatus);

        return sendStatus;
    };
}
